04. Push Changes To A Remote

L1 - Git Push In Theory

Reviewing Commits

Let's take a look at the commits that I have in my Repository.

_The terminal application displaying the log of the project's commits._

The terminal application displaying the log of the project's commits.

I used the following log command to display these commits

$ git log --oneline --graph --decorate --all

These commits are only in the local repository, though. They haven't been sent up to the remote repository yet. When commits are sent to the remote a remote branch indicator will appear in the log. Since there aren't any remote branch indicators we can tell that there are no commits on the remote repository. But just to be 100% certain let's look at the remote repository on GitHub to see if there any commits.

_The remote repository doesn't contain any commits, so GitHub displays the repository's setup screen._

The remote repository doesn't contain any commits, so GitHub displays the repository's setup screen.

Since we haven't sent any commits update to GitHub yet it's still showing us the setup screen to tell us how we can connect our local repository to the remote repository and send some commits. Since this is still the setup screen we can know that there are no commits in the remote repository.

Sending Commits

To send local commits to a remote repository you need to use the git push command. You provide the remote short name and then you supply the name of the branch that contains the commits you want to push:

$ git push <remote-shortname> <branch>

My remote's shortname is origin and the commits that I want to push are on the master branch. So I'll use the following command to send my commits to the remote repository on GitHub:

$ git push origin master

_The terminal application showing the `git push` command. It asks for my username, password (which isn't displayed), and then displays information about what it's doing to send the commits._

The terminal application showing the git push command. It asks for my username, password (which isn't displayed), and then displays information about what it's doing to send the commits.

There a couple of things to notice:

  • Depending on how you have configured GitHub and the remote URL that's being used, you might have to enter your username and password.
    • this will happen if you use the HTTP version of the remote (rather than the ssh version)
    • If you have configured GitHub to use the SSH protocol and have supplied it with your SSH key then you don't need to worry about doing this step. Check the Connecting to GitHub with SSH documentation page if you're interested in using SSH with GitHub.
  • If you have to enter your username and password your username will show up after typing but your password will not. So just keep typing your password and press enter when you're done.
    • If you encounter any errors with your password don't worry it'll just ask you to type it in again
  • Git does some compressing of things to make it smaller and then sends those off to the remote
  • A new branch is created - at the very bottom it says [new branch] and then master -> master

Now let's look at GitHub:

_My entire project is up on GitHub!_

My entire project is up on GitHub!

Our project is up on GitHub - how awesome and easy was that! One cool feature that GitHub does is that it automatically shows the content of the README file which can be extremely helpful.

GitHub also displays a lot of details about our Repository. Right now it's showing that there are:

  • three commits
  • one branch
  • one contributor

_The project's main page on GitHub displays information about the repository._

The project's main page on GitHub displays information about the repository.

Now before we move on, let's just check the local repository to see how it changed after pushing.

Run the following command:

$ git log --oneline --graph --decorate --all

Important: make sure to include the --decorate and --all flags

_The terminal application showing the results of running `git log --oneline --graph --decorate --all`. The new `origin/master` tracking branch now exists._

The terminal application showing the results of running git log --oneline --graph --decorate --all. The new origin/master tracking branch now exists.

We now have a new marker in the output! This marker is origin/master and is called a tracking branch. A tracking branch's name includes the shortname of the remote repository as well as the name of the branch. So the tracking branch origin/master is telling us that the remote origin has a master branch that points to commit 9b7d28f (and includes all of the commits before 9b7d28f). This is really helpful because this means we can track the information of the remote Repository right here in our local one!

One very important thing to know is that this origin/master tracking branch is not a live representation of where the branch exists on the remote repository. If a change is made to the remote repository not by us but by someone else, the origin/master tracking branch in our local repository will not move. We have to tell it to go check for any updates and then it will move. We'll look at how to do this in the next section.

Recap

The git push command is used to send commits from a local repository to a remote repository.

$ git push origin master

The git push command takes:

  • the shortname of the remote repository you want to send commits to
  • the name of the branch that has the commits you want to send